<?php


namespace app\index\model;


use think\Model;

class Member extends Model
{
    public static function getMemberByCustomerId($id)
    {
        $CustomerClass=new Customer();
        $data=$CustomerClass->alias('c')
            ->join('member m','c.id=m.customer_id','left')
            ->where(['c.id'=>$id])
            ->field('c.id customer_id,c.customer_name,c.type,m.id,m.card_id,m.balance')->find();
        return $data;
    }
    public static function addMember($data)
    {
        $memberClass=new self();
        $customerClass=new Customer();
/*        $res=$memberClass->where(['card_id'=>$data['card_id']])->find();
        if ($res){
            return ['status'=>false,'result'=>'该会员卡号已存在'];
        }*/

        $memberClass->startTrans();
        try {
            $balance=$data['num']+$data['sendNum'];
            $add_data=['customer_id'=>$data['customer_id'],'card_id'=>null/*$data['card_id']*/,'balance'=>$balance];
            $member_id=$memberClass->insertGetId($add_data);
            if (!$member_id){
                return ['status'=>false,'result'=>'添加失败'];
            }
            $res=$customerClass->where('id',$data['customer_id'])->update(['type'=>$data['type']]);
            if(!$res){
                return ['status'=>false,'result'=>'客户信息更新失败'];
            }
            $history_data=['customer_id'=>$data['customer_id'],'member_id'=>$member_id,'balance'=>$data['num'],'discount'=>$data['sendNum'],'type'=>1];
            ['status'=>$status,'result'=>$result]=MemberCostHistory::add($history_data);
            if ($status==false){
                return ['status'=>false,'result'=>$result];
            }
            $memberClass->commit();
            return ['status'=>true,'result'=>'添加成功'];
        }catch (\Exception $e){
            $memberClass->rollback();
            trace($e->getMessage(),'error');
            return ['status'=>false,'result'=>'添加失败'.$e->getMessage()];
        }

    }
    public static function updateMember($data)
    {
        $memberClass=new self();
        $memberClass->startTrans();
        try {
            if (!$data['sendNum']){
                $data['sendNum']=0;
            }
            $res=$memberClass->where(['id'=>$data['id']])->find();
            $balance=$data['num']+$data['sendNum']+$res['balance'];
            $result=$memberClass->where('id',$res['id'])->update(['balance'=>$balance]);
            if ($result===false){
                return ['status'=>false,'result'=>'添加失败'];
            }
            $history_data=['customer_id'=>$data['customer_id'],'member_id'=>$data['id'],'balance'=>$data['num'],'discount'=>$data['sendNum'],'type'=>1];
            ['status'=>$status,'result'=>$result]=MemberCostHistory::add($history_data);
            if ($status===false){
                return ['status'=>false,'result'=>$result];
            }
            $memberClass->commit();
            return ['status'=>true,'result'=>'充值成功'];
        }catch (\Exception $e){
            $memberClass->rollback();
            trace($e->getMessage(),'error');
            return ['status'=>false,'result'=>'添加失败'.$e->getMessage()];
        }
    }
}